package com.springboot.film.controller;

import com.springboot.film.entity.Page;
import com.springboot.film.entity.films;
import com.springboot.film.entity.userinfo;
import com.springboot.film.service.filmsService;
import com.springboot.film.service.userinfoService;
import com.springboot.film.unit.CosineSimilarity;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class qiantaiCtrl {

    @Autowired
    private userinfoService userinfoService;
    @Autowired
    private filmsService filmsService;

    //登录
    @RequestMapping("/qiantai_login")
    @ResponseBody
    public String qiantai_login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        JSONObject jsonObject = new JSONObject();
        int i = 0;
        i = userinfoService.checkone(username,password);
        if (i > 0) {
            HttpSession session = request.getSession();
            session.setAttribute("username",username);
            i = userinfoService.getuid(username);
            session.setAttribute("userid",i);
            String usernameC = (String) request.getSession().getAttribute("username");
            jsonObject.put("code", "1");
            jsonObject.put("message", "登录成功");
            return jsonObject.toString();
        }else {
            jsonObject.put("code", "2");
            jsonObject.put("message", "登录失败");
            return jsonObject.toString();
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/adduserinfo")
    @ResponseBody
    public String adduserinfo(userinfo userinfo) {
        userinfoService.add(userinfo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //首页
    @RequestMapping("/home")
    public String films(Model model,HttpSession session) {
        List<films> filmshotList=new ArrayList<films>();
        List<films> filmstimeList=new ArrayList<films>();
        filmshotList=filmsService.getallhotfilms();
        filmstimeList=filmsService.getalltimefilms();
        String username=(String)session.getAttribute("username");
        if(username == null){
            model.addAttribute("login", "nologin");
        }else {
            model.addAttribute("login", username);
        }
        model.addAttribute("hot", filmshotList);
        model.addAttribute("time", filmstimeList);
        return "home";
    }

    //分类
    @RequestMapping("/classify")
    public String filmsclassify(Model model,String pageNow,String type,String updown,HttpSession session) {
        int totalCount = 0;
        List<films> filmsList=new ArrayList<films>();
        Page page=null;
        if(type.equals("0") && updown.equals("down_all")){
            totalCount = filmsService.getCount();
            if(pageNow!=null){
                page=new Page(totalCount, Integer.parseInt(pageNow));
                filmsList=filmsService.findByPage(page.getStartPos(), page.getPageSize());
            }else{
                page=new Page(totalCount, 1);//初始化pageNow为1
                filmsList=filmsService.findByPage(page.getStartPos(), page.getPageSize());
            }
        }else if (type.equals("0")){
            int down_temp = Integer.parseInt(updown);
            totalCount = filmsService.getviplookCount(down_temp);
            if(pageNow!=null){
                page=new Page(totalCount, Integer.parseInt(pageNow));
                filmsList=filmsService.findviplookByPage(down_temp,page.getStartPos(), page.getPageSize());
            }else{
                page=new Page(totalCount, 1);//初始化pageNow为1
                filmsList=filmsService.findviplookByPage(down_temp,page.getStartPos(), page.getPageSize());
            }
        }else if (updown.equals("down_all")){
            totalCount = filmsService.gettypeCount(type);
            if(pageNow!=null){
                page=new Page(totalCount, Integer.parseInt(pageNow));
                filmsList=filmsService.findtypeByPage(type,page.getStartPos(), page.getPageSize());
            }else{
                page=new Page(totalCount, 1);//初始化pageNow为1
                filmsList=filmsService.findtypeByPage(type,page.getStartPos(), page.getPageSize());
            }
        }else{
            int down_temp = Integer.parseInt(updown);
            totalCount = filmsService.getallCount(type,down_temp);
            if(pageNow!=null){
                page=new Page(totalCount, Integer.parseInt(pageNow));
                filmsList=filmsService.findallByPage(type,down_temp,page.getStartPos(), page.getPageSize());
            }else{
                page=new Page(totalCount, 1);//初始化pageNow为1
                filmsList=filmsService.findallByPage(type,down_temp,page.getStartPos(), page.getPageSize());
            }
        }
        String username=(String)session.getAttribute("username");
        if(username == null){
            model.addAttribute("login", "nologin");
        }else {
            model.addAttribute("login", username);
        }
        model.addAttribute("top", type);
        model.addAttribute("down", updown);
        model.addAttribute("type", type+"&updown="+updown);
        model.addAttribute("page", page);
        model.addAttribute("films", filmsList);
        return "classify";
    }

    //我的
    @RequestMapping("/mine")
    public String mine(Model model,HttpSession session) {
        List<userinfo> userinfoList=new ArrayList<userinfo>();
        List<films> myfilms=new ArrayList<films>();
        String username=(String)session.getAttribute("username");
        int userid=(int)session.getAttribute("userid");
        userinfoList = userinfoService.getone(userid);
        if (userinfoService.getfid(userid,1)>0){
            myfilms = filmsService.getmyfilmsall(userid);
            model.addAttribute("myfilms", myfilms);
        }
        int fid=0;
        int[] fid_temp={0,0,0,0};
        for (int i=1; i<5; i++){
            fid=userinfoService.getfid(userid,i);
            if (fid>0){
                fid_temp[i-1] = fid;
            }
        }
        if(fid_temp[3]!=0){
            int[] tuijian=tuijian(fid_temp);
            for (int i=1;i<=tuijian.length;i++){
                model.addAttribute("likefilms"+i, filmsService.getmylike(tuijian[i-1]));
            }
            model.addAttribute("like", "1");
        }else{
            model.addAttribute("like", "0");
        }
        if(username == null){
            model.addAttribute("login", "nologin");
        }else {
            model.addAttribute("login", username);
        }
        model.addAttribute("userinfo", userinfoList);
        return "mine";
    }

    //个性推荐算法
    public int[] tuijian(int[] fid_temp){
        String[] type={"0","0","0","0"};
        String[] content={"0","0","0","0"};
        for (int i=0; i<4; i++){
            if (fid_temp[i]>0){
                type[i] = filmsService.gettype(fid_temp[i]);
                content[i] = filmsService.getcontent(fid_temp[i]);
            }
        }
        int[] max_id={0,0,0,0};
        int like_id=0,
                like_id1_1=filmsService.getid(type[0],0),like_id1_2=filmsService.getid(type[0],1),
                like_id1_3=filmsService.getid(type[0],2),like_id1_4=filmsService.getid(type[0],3),
                like_id2_1=filmsService.getid(type[1],0),like_id2_2=filmsService.getid(type[1],1),
                like_id2_3=filmsService.getid(type[1],2),
                like_id3_1=filmsService.getid(type[2],0),like_id3_2=filmsService.getid(type[2],1),
                like_id4_1=filmsService.getid(type[3],0);

        double like=0.00,
                like1_1=score(content[0],filmsService.getcontent(like_id1_1)),
                like1_2=score(content[0],filmsService.getcontent(like_id1_2)),
                like1_3=score(content[0],filmsService.getcontent(like_id1_3)),
                like1_4=score(content[0],filmsService.getcontent(like_id1_4)),
                like2_1=score(content[1],filmsService.getcontent(like_id2_1)),
                like2_2=score(content[1],filmsService.getcontent(like_id2_2)),
                like2_3=score(content[1],filmsService.getcontent(like_id2_3)),
                like3_1=score(content[2],filmsService.getcontent(like_id3_1)),
                like3_2=score(content[2],filmsService.getcontent(like_id3_2)),
                like4_1=score(content[3],filmsService.getcontent(like_id4_1));
        for (int i=0; i<4; i++){
            if(!"0".equals(type[i])){
                max_id[i] = filmsService.getmaxid(type[i]);
            }
        }
        for (int j=4; j<max_id[0]; j++){
            like_id = filmsService.getid(type[0],j-1);
            like = score(content[0],filmsService.getcontent(like_id));
            if(like!=1 && like1_1!=1 && like1_2!=1 && like1_3!=1 && like1_4!=1){
                if(like>like1_1){
                    like1_1=like;
                    like_id1_1=like_id;
                }else if(like>like1_2){
                    like1_2=like;
                    like_id1_2=like_id;
                }else if(like>like1_3){
                    like1_3=like;
                    like_id1_3=like_id;
                }else if(like>like1_4){
                    like1_4=like;
                    like_id1_4=like_id;
                }else if(like_id1_1==like_id1_2){
                    like1_2=like;
                    like_id1_2=like_id;
                }else if(like_id1_2==like_id1_3 || like_id1_1==like_id1_3){
                    like1_3=like;
                    like_id1_3=like_id;
                }else if(like_id1_3==like_id1_4 || like_id1_1==like_id1_4 || like_id1_2==like_id1_4){
                    like1_4=like;
                    like_id1_4=like_id;
                }
            }else{
                if(like1_1==1){
                    like1_1=like;
                    like_id1_1=like_id;
                }else if(like1_2==1){
                    like1_2=like;
                    like_id1_2=like_id;
                }else if(like1_3==1){
                    like1_3=like;
                    like_id1_3=like_id;
                }else if(like1_4==1){
                    like1_4=like;
                    like_id1_4=like_id;
                }
            }
        }
        for (int j=3; j<max_id[1]; j++){
            like_id = filmsService.getid(type[1],j-1);
            like = score(content[1],filmsService.getcontent(like_id));
            if(like!=1 && like2_1!=1 && like2_2!=1 && like2_3!=1){
                if(like>like2_1){
                    like2_1=like;
                    like_id2_1=like_id;
                }else if(like>like2_2){
                    like2_2=like;
                    like_id2_2=like_id;
                }else if(like>like2_3){
                    like2_3=like;
                    like_id2_3=like_id;
                }else if(like_id2_1==like_id2_2){
                    like2_2=like;
                    like_id2_2=like_id;
                }else if(like_id2_2==like_id2_3 || like_id2_1==like_id2_3){
                    like2_3=like;
                    like_id2_3=like_id;
                }
            }else{
                if(like2_1==1){
                    like2_1=like;
                    like_id2_1=like_id;
                }else if(like2_2==1){
                    like2_2=like;
                    like_id2_2=like_id;
                }else if(like2_3==1){
                    like2_3=like;
                    like_id2_3=like_id;
                }
            }
        }
        for (int j=2; j<max_id[2]; j++){
            like_id = filmsService.getid(type[2],j-1);
            like = score(content[2],filmsService.getcontent(like_id));
            if(like!=1 && like3_1!=1 && like3_2!=1){
                if(like>like3_1){
                    like3_1=like;
                    like_id3_1=like_id;
                }else if(like>like3_2){
                    like3_2=like;
                    like_id3_2=like_id;
                }else if(like_id3_1==like_id3_2){
                    like3_2=like;
                    like_id3_2=like_id;
                }
            }else{
                if(like3_1==1){
                    like3_1=like;
                    like_id3_1=like_id;
                }else if(like3_2==1){
                    like3_2=like;
                    like_id3_2=like_id;
                }
            }
        }
        for (int j=1; j<max_id[3]; j++){
            like_id = filmsService.getid(type[3],j-1);
            like = score(content[3],filmsService.getcontent(like_id));
            if(like!=1 && like4_1!=1){
                if(like>like4_1){
                    like4_1=like;
                    like_id4_1=like_id;
                }
            }else {
                System.out.println(like4_1);
                if(like4_1==1){
                    like4_1=like;
                    like_id4_1=like_id;
                }
            }
        }
        int[] like_id_all={like_id1_1,like_id1_2,like_id1_3,like_id1_4,like_id2_1,like_id2_2,like_id2_3,
                             like_id3_1,like_id3_2,like_id4_1};
        return like_id_all;
    }

    //比较内容相似度
    public double score(String content1,String content2){
        double score=0.00;
        score= CosineSimilarity.getSimilarity(content1,content2);
        return score;
    }

    //更改
    @RequestMapping("/updateuserinfo")
    public String updateuserinfo(userinfo userinfo) {
        userinfoService.update(userinfo);
        return "redirect:/mine";
    }

    @RequestMapping("/quit")
    public String quit(HttpSession session) {
        session.removeAttribute("username");
        session.removeAttribute("userid");
        return "redirect:/home";
    }

    @RequestMapping("/search")
    public String search(String keyword,Model model,String pageNow,HttpSession session) {
        int totalCount = 0;
        List<films> filmsList=new ArrayList<films>();
        Page page=null;
        totalCount = filmsService.getallsearchCount(keyword);
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            filmsList=filmsService.getallsearch(keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            filmsList=filmsService.getallsearch(keyword,page.getStartPos(), page.getPageSize());
        }
        String username=(String)session.getAttribute("username");
        if(username == null){
            model.addAttribute("login", "nologin");
        }else {
            model.addAttribute("login", username);
        }
        model.addAttribute("top", "0");
        model.addAttribute("down", "down_all");
        model.addAttribute("type", "0"+"&keyword="+keyword+"&updown="+"down_all");
        model.addAttribute("page", page);
        model.addAttribute("films", filmsList);
        return "classify";
    }

}
